package com.lczyfz.istep.modules.openstack.api.neutron.impl;


import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.Routers;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Router;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.*;

/**
 * Router�ӿڵ�ʵ����
 */
public class RouterImpl implements Routers {
	public RequestFactory factory = new RequestFactory("neutron");

	@Override
	public List<Router> getRouters(){
		List<Router> routers = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers");
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		System.out.println(body.toString());
		routers = NeutronParse.resolveRouters(body);
		return routers;
	}

	@Override
	public Router getRouterDetail(String router_id){
		Router router = new Router();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers/" + router_id);
		request.setRequestType(RequestType.GET);

		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		System.out.println(body);
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		router = NeutronParse.getRouterDetail(body);

		return router;
	}

	@Override
	public Router createRouter(Router routerinput){
		Router router = new Router();
		JSONObject params = NeutronParse.packageRouter(routerinput);
		System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);

		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		router = NeutronParse.resolveCreateRouter(res);
		return router;
	}
	@Override
	public void deleteRouter(String router_id){
		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers/" + router_id);
		request.setRequestType(RequestType.DELETE);

		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
	}
	@Override
	public Router addRouterInterface(String router_id , String subnet_id,int type){
		Router router = new Router();
		JSONObject params = NeutronParse.packageRouterInterface(subnet_id,type);
		//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers/" + router_id + "/add_router_interface");
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);

		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		router = NeutronParse.resolveAddRouterInterface(res);
		return router;
	}

	@Override
	public Router removeRouterInterface(String router_id , String subnet_id,int type){
		Router router = new Router();
		JSONObject params = NeutronParse.packageRouterInterface(subnet_id, type);
		//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers/" + router_id + "/remove_router_interface");
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);

		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		router = NeutronParse.resolveRemoveRouterInterface(res);
		return router;
	}

	@Override
	public Router updateRouter(String router_id , Router routerupdateinput){
		Router router = new Router();
		JSONObject params = NeutronParse.packageUpdateRouter(routerupdateinput);
		System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers/" + router_id);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);

		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}

		JSONObject res = results.getResponseBody();
		router = NeutronParse.resolveUpdateRouter(res);
		return router;
	}

	@Override
	public Router addRouterList(String router_id, HashMap<String, String> routerList) {
		Router router = getRouterDetail(router_id);
		Router newrouter = new Router();
		Map<String,String> routerListCurrent = router.getRouters();
//		System.out.println(routerListCurrent+"current");
		routerListCurrent.putAll(routerList);
//		System.out.println(routerListCurrent+"current");
		newrouter.setRouters(routerListCurrent);
		Router routerlistUpdate = updateRouter(router_id,newrouter);
		return routerlistUpdate;
	}

	@Override
	public Router removeRouterList(String router_id, HashMap<String, String> routerList) {
		Router router = getRouterDetail(router_id);
		Router newrouter = new Router();
		Map<String,String> routerListCurrent = router.getRouters();
//		System.out.println(routerListCurrent+"current");
		Set keyset = routerList.keySet();
		ArrayList<String> routerListSet = new ArrayList<String>();
		for (Iterator iter = keyset.iterator(); iter.hasNext();){
			String key = (String) iter.next();
			routerListSet.add(key);
		}
		for(String s : routerListSet) {
			routerListCurrent.remove(s);
		}
//		System.out.println(routerListCurrent+"current");
		newrouter.setRouters(routerListCurrent);
		Router routerlistUpdate = updateRouter(router_id,newrouter);
		return routerlistUpdate;
	}

	public boolean checkUniqueName(String routerName){
		return true;
	}
	@Override
	public Router setExGateWay(String router_id, String exNetworkId, String exSubnetId, String ipAddress) {

		Router router = new Router();
		JSONObject params = NeutronParse.packageGateWay(router_id,exNetworkId,exSubnetId,ipAddress);

		Request request = factory.getRequest();
		request.setUrl("/v2.0/routers/" + router_id);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);

		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}

		JSONObject res = results.getResponseBody();
		router = NeutronParse.resolveUpdateRouter(res);
		return router;
	}


	public static void main(String[] args){

		RouterImpl routerdemo = new RouterImpl();
//		System.out.println("·����Ϣ");
//		List<Router> routers = routerdemo.getRouters();
//
		Router router = routerdemo.getRouterDetail("085627ce-0b34-48ca-ad69-ea318f27fa07");
//		try {
//			HashMap<String, String> pp = new HashMap<String, String>();
//			pp.put("10.10.20.0/24","10.10.20.109");
////			pp.put("192.168.1.0/24", "192.168.1.4");
////			Router router1 = routerdemo.addRouterList("085627ce-0b34-48ca-ad69-ea318f27fa07",pp);
//			Router router1 = routerdemo.removeRouterList("085627ce-0b34-48ca-ad69-ea318f27fa07",pp);
//		}catch(Exception e) {
//
//		}
//		for(Router router : routers){
//			System.out.println(router.getRouterId()+" "+router.getRouterName()+" "+router.getRouterStatus()+" "+router.getRouterProject()+" "+router.getRouterTenant());
//			System.out.println(router.getRouters());
//		}
//		System.out.println("·����ϸ��Ϣ");
//		Router router = routerdemo.getRouterDetail("15a13022-d9c2-4506-b09a-80ff6f9091a1");
//		System.out.println(router.getRouterId()+" "+router.getRouterName()+" "+router.getRouterStatus()+" "+router.getRouterProject()+" "+router.getRouterTenant()+" || "+router.getRouterNetworkId()+" "+router.getRouterSubnetId());
//		System.out.println(router.getRouters());
//		System.out.println("����·��");
//		try {
//			Router routertt = new Router();
//			routertt.setRouterName("router444");
//
//	//		routertt.setRouterNetworkId("43d20043-4808-420a-8706-ffe3e64c0cd5");
//			Router router_new = routerdemo.createRouter(routertt);
//			System.out.println(router_new.getRouterId()+" "+router_new.getRouterName()+" "+router_new.getRouterStatus()+" "+router_new.getRouterProject()+" "+router_new.getRouterTenant()+" || ");
//		}catch(Exception e) {
//
//		}
		//	System.out.println("ɾ��·��");
		//	routerdemo.deleteRouter("acab13fd-4f38-42c8-aa20-981d5cc4aeec");
//		System.out.println("�������");
//		Router router_add_interface = routerdemo.addRouterInterface("15a13022-d9c2-4506-b09a-80ff6f9091a1","a46fd12f-0607-41ec-8411-db7ae8e41ce4");
//		System.out.println(router_add_interface.getRouterId()+" "+router_add_interface.getRouterProject()+" "+router_add_interface.getRouterTenant()+" || "+router_add_interface.getRouterNetworkId()+" "+router_add_interface.getRouterSubnetId());
//
		//	System.out.println("�Ƴ�����");
		//	Router router_remove_interface = routerdemo.removeRouterInterface("e836d84a-dfcb-4a61-89fc-aa805443daa9","5294c691-4a70-4391-897a-25058dc05e6d");
		//	System.out.println(router_remove_interface.getRouterId()+" "+router_remove_interface.getRouterProject()+" "+router_remove_interface.getRouterTenant()+" || "+router_remove_interface.getRouterNetworkId()+" "+router_remove_interface.getRouterSubnetId());
//		System.out.println("����·��");
		Router routertt = new Router();
		try {
			HashMap<String,String> pp = new HashMap<String,String>();
			pp.put("192.168.0.0/24","192.168.0.9");
			pp.put("10.10.20.0/24","10.10.20.109");
			routertt.setRouters(pp);
			Router router_update = routerdemo.updateRouter("085627ce-0b34-48ca-ad69-ea318f27fa07",routertt);
//			System.out.println(router_update.getRouterId()+" "+router.getRouterName()+" "+router.getRouterStatus()+" "+router_update.getRouterProject()+" "+router_update.getRouterTenant()+" || "+router_update.getRouterNetworkId()+" "+router_update.getRouterSubnetId());
		}catch(Exception e) {

		}

	}
}
